home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / page-ext.el < prev    next >
Text File  |  1993-11-14  |  28KB  |  770 lines

  1. ;;; page-ext.el --- extended page handling commands
  2.  
  3. ;; You may use these commands to handle an address list or other
  4. ;; small data base.
  5.  
  6. ;; Copyright (C) 1990, 1991, 1993 Free Software Foundation
  7.  
  8. ;; Maintainer: Robert J. Chassell <bob@gnu.ai.mit.edu>
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26.  
  27. ;;; Summary
  28.  
  29. ; The current page commands are:
  30.  
  31. ;     forward-page         C-x ]
  32. ;     backward-page        C-x [
  33. ;     narrow-to-page       C-x p
  34. ;     count-lines-page     C-x l
  35. ;     mark-page            C-x C-p  (change this to C-x C-p C-m)
  36. ;     sort-pages           not bound
  37. ;     what-page            not bound
  38.  
  39. ; The new page handling commands all use `C-x C-p' as a prefix.  This
  40. ; means that the key binding for `mark-page' must be changed.
  41. ; Otherwise, no other changes are made to the current commands or
  42. ; their bindings.
  43.  
  44. ; New page handling commands:
  45.  
  46. ;     next-page                        C-x C-p C-n
  47. ;     previous-page                    C-x C-p C-p
  48. ;     search-pages                     C-x C-p C-s
  49. ;     add-new-page                     C-x C-p C-a
  50. ;     sort-pages-buffer                C-x C-p s      
  51. ;     set-page-delimiter               C-x C-p C-l
  52. ;     pages-directory                  C-x C-p C-d   
  53. ;     pages-directory-for-addresses    C-x C-p d
  54. ;        pages-directory-goto          C-c C-c
  55.  
  56.  
  57. ;;; Using the page commands
  58.  
  59. ; The page commands are helpful in several different contexts.  For
  60. ; example, programmers often divide source files into sections using the
  61. ; `page-delimiter'; you can use the `pages-directory' command to list
  62. ; the sections.
  63.  
  64. ; You may change the buffer local value of the `page-delimiter' with
  65. ; the `set-page-delimiter' command.  This command is bound to `C-x C-p
  66. ; C-l' The command prompts you for a new value for the page-delimiter.
  67. ; Called with a prefix-arg, the command resets the value of the
  68. ; page-delimiter to its original value.
  69.  
  70. ; You may set several user options:
  71. ;
  72. ;   The `pages-directory-buffer-narrowing-p' variable causes the
  73. ;   `pages-directory-goto' command to narrow to the destination page.
  74. ;
  75. ;   The `pages-directory-for-adding-page-narrowing-p' variable, causes the
  76. ;   `add-new-page' command to narrow to the new entry.
  77. ;
  78. ;   The `pages-directory-for-adding-new-page-before-current-page-p' variable
  79. ;   causes the `add-new-page' command to insert a new page before current
  80. ;   page.
  81. ;
  82. ; These variables are true by default.
  83. ;
  84. ; Additional, addresses-related user options are described in the next page
  85. ; of this file.
  86.  
  87.  
  88. ;;; Handling an address list or small data base
  89.  
  90. ; You may use the page commands to handle an address list or other
  91. ; small data base.  Put each address or entry on its own page.  The
  92. ; first line of text in each page is a `header line' and is listed by
  93. ; the `pages-directory' or `pages-directory-for-addresses' command.
  94.  
  95. ; Specifically:
  96. ;   1. Begin each entry with a `page-delimiter' (which is, by default,
  97. ;      `^L' at the beginning of the line).
  98. ;   2. The first line of text in each entry is the `heading line'; it
  99. ;      will appear in the pages-directory-buffer which is constructed
  100. ;      using the `C-x C-p C-d' (pages-directory) command or the `C-x
  101. ;      C-p d' (pages-directory-for-addresses) command.
  102. ;      The heading line may be on the same line as the page-delimiter
  103. ;      or it may follow after.  It is the first non-blank line on the
  104. ;      page.  Conventionally, the heading line is placed on the line
  105. ;      immediately following the line containing page-delimiter.
  106. ;
  107. ;   3. Follow the heading line with the body of the entry.  The body
  108. ;      extends up to the next `page-delimiter'.  The body may be of any
  109. ;      length.  It is conventional to place a blank line after the last
  110. ;      line of the body.
  111.  
  112. ; For example, a file might look like this:
  113. ;     FSF
  114. ;     Free Software Foundation
  115. ;     675 Massachusetts Avenue
  116. ;     Cambridge, MA 02139  USA
  117. ;     (617) 876-3296
  118. ;     gnu@prep.ai.mit.edu
  119. ;     
  120. ;     House Subcommittee on Intellectual Property,
  121. ;     U.S. House of Representatives,
  122. ;     Washington, DC  20515
  123. ;     
  124. ;     Congressional committee concerned with permitting or preventing
  125. ;     monopolistic restrictions on the use of software technology.
  126. ;     
  127. ;     George Lakoff
  128. ;     ``Women, Fire, and Dangerous Things:
  129. ;     What Categories Reveal about the Mind''
  130. ;     1987, Univ. of Chicago Press
  131. ;     About philosophy, Whorfian effects, and linguistics.
  132. ;       
  133. ;     OBI   (On line text collection.)
  134. ;     Open Book Initiative
  135. ;     c/o Software Tool & Die
  136. ;     1330 Beacon St, Brookline, MA 02146 USA
  137. ;     (617) 739-0202 
  138. ;     obi@world.std.com
  139.  
  140. ; In this example, the heading lines are:
  141. ;
  142. ;     FSF
  143. ;     House Subcommittee on Intellectual Property
  144. ;     George Lakoff
  145. ;     OBI (On line text collection.)
  146.  
  147. ; The `C-x C-p s' (sort-pages-buffer) command sorts the entries in the
  148. ; buffer alphabetically.
  149.  
  150. ; You may use any of the page commands, including the `next-page',
  151. ; `previous-page', `add-new-page', `mark-page', and `search-pages'
  152. ; commands.
  153.  
  154. ; You may use either the `C-x C-p d' (pages-directory-for-addresses)
  155. ; or the `C-x C-p C-d' (pages-directory) command to construct and
  156. ; dislay a directory of all the heading lines.
  157.  
  158. ; In the directory, you may position the cursor over a heading line
  159. ; and type `C-c C-c' (pages-directory-goto) to go to the entry to
  160. ; which it refers in the pages buffer.
  161.  
  162. ; You can type `C-c C-p C-a' (add-new-page) to add a new entry in the
  163. ; pages buffer or address file.  This is the same command you use to
  164. ; add a new entry when you are in the pages buffer or address file.
  165.  
  166. ; If you wish, you may create several different directories,
  167. ; one for each different buffer.
  168.  
  169. ;; `pages-directory-for-addresses' in detail
  170.  
  171. ; The `pages-directory-for-addresses' assumes a default addresses
  172. ; file.  You do not need to specify the addresses file but merely type
  173. ; `C-x C-p d' from any buffer.  The command finds the file, constructs
  174. ; a directory for it, and switches you to the directory.  If you call
  175. ; the command with a prefix arg, `C-u C-x C-p d', it prompts you for a
  176. ; file name.
  177.  
  178. ; You may customize the addresses commands:
  179.  
  180. ;   The `pages-addresses-file-name' variable determines the name of
  181. ;   the addresses file; by default it is "~/addresses".
  182.  
  183. ;   The `pages-directory-for-addresses-goto-narrowing-p' variable
  184. ;   determines whether `pages-directory-goto' narrows the addresses
  185. ;   buffer to the entry, which it does by default.
  186.  
  187. ;   The `pages-directory-for-addresses-buffer-keep-windows-p' variable
  188. ;   determines whether `pages-directory-for-addresses' deletes other
  189. ;   windows to show as many lines as possible on the screen or works
  190. ;   in the usual Emacs manner and keeps other windows.  Default is to
  191. ;   keep other windows.
  192.  
  193. ;   The `pages-directory-for-adding-addresses-narrowing-p' variable
  194. ;   determines whether `pages-directory-for-addresses' narrows the
  195. ;   addresses buffer to a new entry when you are adding that entry.
  196. ;   Default is to narrow to new entry, which means you see a blank
  197. ;   screen before you write the new entry.
  198.  
  199. ;; `pages-directory' in detail
  200.  
  201. ; Call the `pages-directory' command from the buffer for which you
  202. ; want a directory created; it creates a directory for the buffer and
  203. ; pops you to the directory.
  204.  
  205. ; The `pages-directory' command has several options:
  206.  
  207. ;   Called with a prefix arg, `C-u C-x C-p C-d', the `pages-directory'
  208. ;   prompts you for a regular expression and only lists only those
  209. ;   header lines that are part of pages that contain matches to the
  210. ;   regexp.  In the example above, `C-u C-x C-p C-d 617 RET' would
  211. ;   match the telephone area code of the first and fourth entries, so
  212. ;   only the header lines of those two entries would appear in the
  213. ;   pages-directory-buffer.
  214. ;   Called with a numeric argument, the `pages-directory' command
  215. ;   lists the number of lines in each page.  This is helpful when you
  216. ;   are printing hardcopy.  
  217.  
  218. ;   Called with a negative numeric argument, the `pages-directory'
  219. ;   command lists the lengths of pages whose contents match a regexp.
  220.  
  221. ;;; Code:
  222.  
  223.  
  224. ;;; Customarily customizable variable definitions
  225.  
  226. (defvar pages-directory-buffer-narrowing-p t
  227.   "*If non-nil, `pages-directory-goto' narrows pages buffer to entry.")
  228.  
  229. (defvar pages-directory-for-adding-page-narrowing-p t
  230.   "*If non-nil, `add-new-page' narrows page buffer to new entry.")
  231.  
  232. (defvar pages-directory-for-adding-new-page-before-current-page-p t
  233.   "*If non-nil, `add-new-page' inserts new page before current page.")
  234.  
  235.  
  236. ;;; Addresses related variables
  237.  
  238. (defvar pages-addresses-file-name "~/addresses"
  239.   "*Standard name for file of addresses. Entries separated by page-delimiter.
  240. Used by `pages-directory-for-addresses' function.")
  241.  
  242. (defvar pages-directory-for-addresses-goto-narrowing-p t
  243.   "*If non-nil, `pages-directory-goto' narrows addresses buffer to entry.")
  244.  
  245. (defvar pages-directory-for-addresses-buffer-keep-windows-p t
  246.   "*If nil, `pages-directory-for-addresses' deletes other windows.")
  247.  
  248. (defvar pages-directory-for-adding-addresses-narrowing-p t
  249.   "*If non-nil, `add-new-page' narrows addresses buffer to new entry.")
  250.  
  251.  
  252. ;;; Key bindings for page handling functions
  253.  
  254. (global-unset-key "\C-x\C-p")
  255.  
  256. (defvar ctl-x-ctl-p-map (make-sparse-keymap)
  257.   "Keymap for subcommands of C-x C-p, which are for page handling.")
  258.  
  259. (define-key ctl-x-map "\C-p" 'ctl-x-ctl-p-prefix)
  260. (fset 'ctl-x-ctl-p-prefix ctl-x-ctl-p-map)
  261.  
  262. (define-key ctl-x-ctl-p-map "\C-n" 'next-page)
  263. (define-key ctl-x-ctl-p-map "\C-p" 'previous-page)
  264. (define-key ctl-x-ctl-p-map "\C-a" 'add-new-page)
  265. (define-key ctl-x-ctl-p-map "\C-m" 'mark-page)
  266. (define-key ctl-x-ctl-p-map "\C-s" 'search-pages)
  267. (define-key ctl-x-ctl-p-map "s"    'sort-pages-buffer)
  268. (define-key ctl-x-ctl-p-map "\C-l" 'set-page-delimiter)
  269. (define-key ctl-x-ctl-p-map "\C-d" 'pages-directory)
  270. (define-key ctl-x-ctl-p-map "d"    'pages-directory-for-addresses)
  271.  
  272.  
  273. ;;; Page movement function definitions
  274.  
  275. (defun next-page (&optional count)
  276.   "Move to the next page bounded by the `page-delimiter' variable.
  277. With arg (prefix if interactive), move that many pages."
  278.   (interactive "p")
  279.   (or count (setq count 1))
  280.   (widen)
  281.   ;; Cannot use forward-page because of problems at page boundaries.
  282.   (while (and (> count 0) (not (eobp)))
  283.     (if (re-search-forward page-delimiter nil t)
  284.         nil
  285.       (goto-char (point-max)))
  286.     (setq count (1- count)))
  287.   ;; If COUNT is negative, we want to go back -COUNT + 1 page boundaries.
  288.   ;; The first page boundary we reach is the top of the current page,
  289.   ;; which doesn't count.
  290.   (while (and (< count 1) (not (bobp)))
  291.     (if (re-search-backward page-delimiter nil t)
  292.     (goto-char (match-beginning 0))
  293.       (goto-char (point-min)))
  294.     (setq count (1+ count)))
  295.   (narrow-to-page)
  296.   (goto-char (point-min))
  297.   (recenter 0))
  298.  
  299. (defun previous-page (&optional count)
  300.   "Move to the previous page bounded by the `page-delimiter' variable.
  301. With arg (prefix if interactive), move that many pages."
  302.   (interactive "p")
  303.   (or count (setq count 1))
  304.   (next-page (- count)))
  305.  
  306.  
  307. ;;; Adding and searching pages
  308.  
  309. (defun add-new-page (header-line)
  310.   "Insert new page.  Prompt for header line.
  311.  
  312. If point is in the pages directory buffer, insert the new page in the
  313. buffer associated with the directory.
  314.  
  315. Insert the new page just before current page if
  316.   pages-directory-for-adding-new-page-before-current-page-p  variable
  317. is non-nil.  Else insert at exact location of point.
  318.  
  319. Narrow to new page if
  320.   pages-directory-for-adding-page-narrowing-p variable
  321. is non-nil.
  322.  
  323. Page begins with a `^L' as the default page-delimiter.  
  324. Use \\[set-page-delimiter] to change the page-delimiter.
  325. Point is left in the body of page."
  326.   (interactive "sHeader line: ")
  327.   (widen)
  328.   ;; If in pages directory buffer
  329.   (if (eq major-mode 'pages-directory-mode) 
  330.       (progn
  331.         ;; Add new page before or after current page?
  332.         (if pages-directory-for-adding-new-page-before-current-page-p
  333.             (pages-directory-goto)
  334.           (pages-directory-goto)
  335.           (forward-page)
  336.           (or (eobp) (forward-line -1)))))
  337.   (widen)
  338.   ;; Move point before current delimiter if desired.
  339.   (and pages-directory-for-adding-new-page-before-current-page-p
  340.        (if (re-search-backward page-delimiter nil t)
  341.            (goto-char (match-beginning 0))
  342.          ;; If going to beginning of file, insert a page-delimiter
  343.          ;; before current first page.
  344.          (goto-char (point-min))
  345.          (insert
  346.           (format "%s\n"
  347.                   ;; Remove leading `^' from page-delimiter string
  348.                   (if (eq '^ (car (read-from-string page-delimiter)))
  349.                       (substring page-delimiter 1))))         
  350.          (goto-char (point-min))))
  351.   ;; Insert page delimiter at beginning of line.
  352.   (if (not (looking-at "^."))   (forward-line 1))
  353.   (insert (format "%s\n%s\n\n\n" 
  354.                   (if (eq '^ (car (read-from-string page-delimiter)))
  355.                       (substring page-delimiter 1))
  356.                   header-line))
  357.   (forward-line -1)
  358.   (and pages-directory-for-adding-page-narrowing-p (narrow-to-page)))
  359.  
  360. (defvar pages-last-search nil
  361.   "Value of last regexp searched for.  Initially, nil.")
  362.  
  363. (defun search-pages (regexp)
  364.   "Search for REGEXP, starting from point, and narrow to page it is in."
  365.   (interactive (list
  366.                 (read-string
  367.                  (format "Search for `%s' (end with RET): "
  368.                          (or pages-last-search "regexp")))))
  369.   (if (equal regexp "")
  370.       (setq regexp pages-last-search)
  371.     (setq pages-last-search regexp))
  372.   (widen)
  373.   (re-search-forward regexp)
  374.   (narrow-to-page))
  375.  
  376.  
  377. ;;; Sorting pages
  378.  
  379. (autoload 'sort-subr "sort" "Primary function for sorting." t nil)
  380.  
  381. (defun sort-pages-in-region (reverse beg end)
  382.   "Sort pages in region alphabetically.  Prefix arg means reverse order.  
  383.  
  384. Called from a program, there are three arguments:
  385. REVERSE (non-nil means reverse order), BEG and END (region to sort)."
  386.  
  387. ;;; This sort function handles ends of pages differently than
  388. ;;; `sort-pages' and works better with lists of addresses and similar
  389. ;;; files.
  390.  
  391.   (interactive "P\nr")
  392.   (save-restriction
  393.     (narrow-to-region beg end)
  394.     (goto-char (point-min))
  395.     ;;; `sort-subr' takes three arguments
  396.     (sort-subr reverse
  397.  
  398.                ;; NEXTRECFUN is called with point at the end of the
  399.                ;; previous record. It moves point to the start of the
  400.                ;; next record.
  401.            (function (lambda ()
  402.                            (re-search-forward page-delimiter nil t)
  403.                            (skip-chars-forward " \t\n")
  404.                            ))
  405.  
  406.                ;; ENDRECFUN is is called with point within the record.
  407.                ;; It should move point to the end of the record.
  408.            (function (lambda ()
  409.                            (if (re-search-forward
  410.                                 page-delimiter
  411.                                 nil
  412.                                 t)
  413.                                (goto-char (match-beginning 0))
  414.                              (goto-char (point-max))))))))
  415.  
  416. (defun sort-pages-buffer (&optional reverse)
  417.   "Sort pages alphabetically in buffer.  Prefix arg means reverse order. 
  418. \(Non-nil arg if not interactive.\)"
  419.  
  420.   (interactive "P")
  421.   (or reverse (setq reverse nil))
  422.   (widen)
  423.   (let ((beginning (point-min))
  424.         (end (point-max)))
  425.     (sort-pages-in-region reverse beginning end)))
  426.  
  427.  
  428. ;;; Pages directory ancillary definitions
  429.  
  430. (defvar pages-directory-previous-regexp nil
  431.   "Value of previous regexp used by `pages-directory'.
  432. \(This regular expression may be used to select only those pages that
  433. contain matches to the regexp.\)")
  434.  
  435. (defvar pages-buffer nil
  436.   "The buffer for which the pages-directory function creates the directory.")
  437.  
  438. (defvar pages-directory-prefix "*Directory for:"
  439.   "Prefix of name of temporary buffer for pages-directory.")
  440.  
  441. (defvar pages-pos-list nil
  442.   "List containing the positions of the pages in the pages-buffer.")
  443.  
  444. (defvar pages-directory-map nil
  445.   "Keymap for the pages-directory-buffer.")
  446.  
  447. (if pages-directory-map
  448.     ()
  449.   (setq pages-directory-map (make-sparse-keymap))
  450.   (define-key pages-directory-map "\C-c\C-c"
  451.     'pages-directory-goto)
  452.   (define-key pages-directory-map "\C-c\C-p\C-a" 'add-new-page))
  453.  
  454. (defvar original-page-delimiter "^ "
  455.   "Default page delimiter.")
  456.  
  457. (defun set-page-delimiter (regexp reset-p)
  458.   "Set buffer local value of page-delimiter to REGEXP.
  459. Called interactively with a prefix argument, reset `page-delimiter' to
  460. its original value.
  461.  
  462. In a program, non-nil second arg causes first arg to be ignored and
  463. resets the page-delimiter to the original value."
  464.   
  465.   (interactive
  466.    (if current-prefix-arg
  467.        (list original-page-delimiter "^ ")
  468.      (list (read-string "Set page-delimiter to regexp: " page-delimiter)
  469.            nil)))
  470.   (make-local-variable 'original-page-delimiter)
  471.   (make-local-variable 'page-delimiter)
  472.   (setq original-page-delimiter
  473.         (or original-page-delimiter page-delimiter))
  474.   (if (not reset-p)
  475.       (setq page-delimiter regexp)
  476.     (setq page-delimiter original-page-delimiter))
  477.   (if (interactive-p)
  478.       (message "The value of `page-delimiter' is now: %s" page-delimiter)))
  479.  
  480.  
  481. ;;; Pages directory main definitions
  482.  
  483. (defun pages-directory
  484.   (pages-list-all-headers-p count-lines-p &optional regexp)
  485.   "Display a directory of the page headers in a temporary buffer.
  486. A header is the first non-blank line after the page-delimiter.
  487. \\[pages-directory-mode]
  488. You may move point to one of the lines in the temporary buffer,
  489. then use \\<pages-directory-goto> to go to the same line in the pages buffer.
  490.  
  491. In interactive use:
  492.  
  493.     1. With no prefix arg, display all headers.
  494.  
  495.     2. With prefix arg, display the headers of only those pages that
  496.        contain matches to a regular expression for which you are
  497.        prompted.
  498.  
  499.     3. With numeric prefix arg, for every page, print the number of
  500.        lines within each page.
  501.  
  502.     4. With negative numeric prefix arg, for only those pages that
  503.        match a regular expression, print the number of lines within
  504.        each page.
  505.  
  506. When called from a program, non-nil first arg means list all headers;
  507. non-nil second arg means print numbers of lines in each page; if first
  508. arg is nil, optional third arg is regular expression.
  509.  
  510. If the buffer is narrowed, the `pages-directory' command creates a
  511. directory for only the accessible portion of the buffer."
  512.   
  513.   (interactive
  514.    (cond ((not current-prefix-arg)
  515.           (list t nil nil))
  516.          ((listp current-prefix-arg) 
  517.           (list nil
  518.                 nil
  519.                 (read-string
  520.                  (format "Select according to `%s' (end with RET): "
  521.                          (or pages-directory-previous-regexp "regexp")))))
  522.          ((> (prefix-numeric-value current-prefix-arg) 0)
  523.           (list t t nil))
  524.          ((< (prefix-numeric-value current-prefix-arg) 0)          
  525.           (list nil
  526.                 t
  527.                 (read-string
  528.                  (format "Select according to `%s' (end with RET): "
  529.                          (or pages-directory-previous-regexp "regexp")))))))
  530.   
  531.   (if (equal regexp "")
  532.       (setq regexp pages-directory-previous-regexp)
  533.     (setq pages-directory-previous-regexp regexp))
  534.   
  535.   (if (interactive-p)
  536.       (message "Creating directory for: %s "
  537.                (buffer-name)))
  538.   
  539.   (let ((target-buffer (current-buffer))
  540.         (pages-directory-buffer
  541.          (concat pages-directory-prefix " " (buffer-name) " "))
  542.         (linenum 1)
  543.         (pages-buffer-original-position (point))
  544.         (pages-buffer-original-page 0))
  545.     
  546.     ;; `with-output-to-temp-buffer' binds the value of the variable
  547.     ;; `standard-output' to the buffer named as its first argument,
  548.     ;; but does not switch to that buffer.
  549.     (with-output-to-temp-buffer pages-directory-buffer
  550.       (save-excursion
  551.         (set-buffer standard-output)
  552.         (pages-directory-mode)
  553.         (insert
  554.          "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n)
  555.         (setq pages-buffer target-buffer)
  556.         (setq pages-pos-list nil))
  557.       
  558.       (if pages-list-all-headers-p
  559.           
  560.           ;; 1. If no prefix argument, list all headers
  561.           (save-excursion
  562.             (goto-char (point-min))
  563.             
  564.             ;; (a) Point is at beginning of buffer; but the first
  565.             ;;     page may not begin with a page-delimiter
  566.             (save-restriction
  567.               ;; If page delimiter is at beginning of buffer, skip it
  568.               (if (and (save-excursion
  569.                          (re-search-forward page-delimiter nil t))
  570.                        (= 1 (match-beginning 0)))
  571.                   (goto-char (match-end 0)))
  572.               (narrow-to-page)
  573.               (pages-copy-header-and-position count-lines-p))
  574.             
  575.             ;; (b) Search within pages buffer for next page-delimiter
  576.             (while (re-search-forward page-delimiter nil t)
  577.               (pages-copy-header-and-position count-lines-p)))
  578.         
  579.         ;; 2. Else list headers whose pages match regexp.
  580.         (save-excursion
  581.           ;; REMOVED  save-restriction  AND  widen  FROM HERE
  582.           (goto-char (point-min))
  583.           
  584.           ;; (a) Handle first page
  585.           (save-restriction
  586.             (narrow-to-page)
  587.             ;; search for selection regexp
  588.             (if (save-excursion (re-search-forward regexp nil t))
  589.                 (pages-copy-header-and-position count-lines-p)))
  590.           
  591.           ;; (b) Search for next page-delimiter
  592.           (while (re-search-forward page-delimiter nil t)
  593.             (save-restriction
  594.               (narrow-to-page)
  595.               ;; search for selection regexp
  596.               (if (save-excursion (re-search-forward regexp nil t))
  597.                   (pages-copy-header-and-position count-lines-p)
  598.                 )))))
  599.       
  600.       (set-buffer standard-output)
  601.       ;; Put positions in increasing order to go with buffer.
  602.       (setq pages-pos-list (nreverse pages-pos-list))
  603.       (if (interactive-p)
  604.           (message "%d matching lines in: %s"
  605.                    (length pages-pos-list) (buffer-name target-buffer))))
  606.     (pop-to-buffer pages-directory-buffer)
  607.     (sit-for 0)  ; otherwise forward-line fails if N > window height.
  608.     (forward-line (if (= 0 pages-buffer-original-page)
  609.                       1
  610.                     pages-buffer-original-page))))
  611.  
  612. (defun pages-copy-header-and-position (count-lines-p)
  613.   "Copy page header and its position to the Pages Directory.
  614. Only arg non-nil, count lines in page and insert before header.
  615. Used by `pages-directory' function."
  616.   
  617.   (let (position line-count)
  618.  
  619.     (if count-lines-p
  620.         (save-excursion
  621.           (save-restriction
  622.             (narrow-to-page)
  623.             (setq line-count (count-lines (point-min) (point-max))))))
  624.  
  625.     ;; Keep track of page for later cursor positioning
  626.     (if (<= (point) pages-buffer-original-position)
  627.         (setq pages-buffer-original-page
  628.               (1+ pages-buffer-original-page)))
  629.     
  630.     (save-excursion
  631.       ;; go to first non-blank char after the page-delimiter
  632.       (skip-chars-forward " \t\n")     
  633.       ;; set the marker here; this the place to which the
  634.       ;; `pages-directory-goto' command will go 
  635.       (setq position (make-marker))
  636.       (set-marker position (point))
  637.       (let ((start (point))
  638.             (end (save-excursion (end-of-line) (point))))
  639.         ;; change to directory buffer
  640.         (set-buffer standard-output)
  641.         ;; record page position 
  642.         (setq pages-pos-list (cons position pages-pos-list))
  643.         ;; insert page header
  644.         (insert-buffer-substring target-buffer start end))
  645.       
  646.       (if count-lines-p
  647.           (save-excursion
  648.             (beginning-of-line)
  649.             (insert (format "%3d: " line-count))))
  650.       
  651.       (terpri))
  652.     (forward-line 1)))
  653.  
  654. (defun pages-directory-mode ()
  655.   "Mode for handling the pages-directory buffer.
  656.  
  657. Move point to one of the lines in this buffer, then use \\[pages-directory-goto] to go
  658. to the same line in the pages buffer."
  659.  
  660.   (kill-all-local-variables)
  661.   (use-local-map pages-directory-map)
  662.   (setq major-mode 'pages-directory-mode)
  663.   (setq mode-name "Pages-Directory")
  664.   (make-local-variable 'pages-buffer)
  665.   (make-local-variable 'pages-pos-list)
  666.   (make-local-variable 'pages-directory-buffer-narrowing-p))
  667.  
  668. (defun pages-directory-goto ()
  669.   "Go to the corresponding line in the pages buffer."
  670.  
  671. ;;; This function is mostly a copy of `occur-mode-goto-occurrence'
  672.  
  673.   (interactive)
  674.   (if (or (not pages-buffer)
  675.       (not (buffer-name pages-buffer)))
  676.       (progn
  677.     (setq pages-buffer nil
  678.           pages-pos-list nil)
  679.     (error "Buffer in which pages were found is deleted.")))
  680.   (beginning-of-line)
  681.   (let* ((pages-number (1- (count-lines (point-min) (point))))
  682.      (pos (nth pages-number pages-pos-list))
  683.          (end-of-directory-p (eobp))
  684.          (narrowing-p  pages-directory-buffer-narrowing-p))
  685.     (pop-to-buffer pages-buffer)  
  686.     (widen)
  687.     (if end-of-directory-p
  688.         (goto-char (point-max))
  689.       (goto-char (marker-position pos)))
  690.     (if narrowing-p (narrow-to-page))))
  691.  
  692.  
  693. ;;; The `pages-directory-for-addresses' function and ancillary code
  694.  
  695. (defun pages-directory-for-addresses (&optional filename)
  696.   "Find addresses file and display its directory.
  697. By default, create and display directory of `pages-addresses-file-name'.
  698. Optional argument is FILENAME.  In interactive use, with prefix
  699. argument, prompt for file name and provide completion.
  700.  
  701. Move point to one of the lines in the displayed directory,
  702. then use \\[pages-directory-goto] to go to the same line
  703. in the addresses buffer.
  704.  
  705. If    pages-directory-for-addresses-goto-narrowing-p    is non-nil,
  706. `pages-directory-goto' narrows addresses buffer to entry.
  707.  
  708. If    pages-directory-for-addresses-buffer-keep-windows-p     is nil,
  709. this command deletes other windows when it displays the addresses 
  710. directory."
  711.  
  712.   (interactive
  713.    (list (if current-prefix-arg
  714.              (read-file-name "Filename: " pages-addresses-file-name))))
  715.  
  716.   (if (interactive-p)
  717.       (message "Creating directory for: %s "
  718.                (or filename pages-addresses-file-name)))
  719.   (if (file-exists-p (or filename pages-addresses-file-name))
  720.       (progn
  721.         (set-buffer
  722.          (find-file-noselect
  723.           (expand-file-name
  724.            (or filename pages-addresses-file-name))))
  725.         (widen)
  726.         (pages-directory t nil nil)
  727.         (pages-directory-address-mode)
  728.         (setq pages-directory-buffer-narrowing-p
  729.               pages-directory-for-addresses-goto-narrowing-p)
  730.         (or pages-directory-for-addresses-buffer-keep-windows-p
  731.             (delete-other-windows))
  732.         (save-excursion
  733.           (goto-char (point-min))
  734.           (delete-region (point) (save-excursion (end-of-line) (point)))
  735.           (insert
  736.            "=== Address List Directory: use `C-c C-c' to go to page under cursor. ===")
  737.           (set-buffer-modified-p nil)
  738.           ))
  739.     (error "No addresses file found!")))
  740.  
  741. (defun pages-directory-address-mode ()
  742.   "Mode for handling the Addresses Directory buffer.
  743.  
  744. Move point to one of the lines in this buffer,
  745. then use \\[pages-directory-goto] to go
  746. to the same line in the pages buffer."
  747.  
  748.   (use-local-map pages-directory-map)
  749.   (setq major-mode 'pages-directory-address-mode)
  750.   (setq mode-name "Addresses Directory")
  751.   (make-local-variable 'pages-buffer)
  752.   (make-local-variable 'pages-pos-list)
  753.   (make-local-variable 'pages-directory-buffer-narrowing-p))
  754.  
  755.  
  756. ;;; Place `provide' at end of file.
  757. (provide 'page-ext)
  758.  
  759. ;;;;;;;;;;;;;;;; end of page-ext.el ;;;;;;;;;;;;;;;;
  760.  
  761.